<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <link rel="stylesheet" href="../component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">部门名</label>
                    <div class="layui-input-inline">
                        <input type="text" id="departmentname" name="departmentname" required=false placeholder="请输入部门名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <a id="searchname" class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="department-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </a>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i id="iReset" class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="department-table" lay-filter="department-table"></table>
    </div>
</div>

<script type="text/html" id="department-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="insertdepartment">新增部门</button>
    </div>
</script>

<script type="text/html" id="department-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="insert_form">
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">ID</label>
                    <div class="layui-input-block">
                        <input type="text" id="insert_id" name="insert_id" lay-verify="title"
                               autocomplete="off" placeholder="请输入ID" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block">
                        <input type="text" id="insert_name" name="insert_name" lay-verify="title"
                               autocomplete="off" placeholder="请输入部门" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">详情</label>
                    <div class="layui-input-block">
                        <input type="text" id="insert_description" name="insert_description" lay-verify="title"
                               autocomplete="off" placeholder="请输入详情" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">领导人ID</label>
                    <div class="layui-input-block">
                        <input type="text" id="insert_leader_id" name="insert_leader_id" lay-verify="title"
                               autocomplete="off" placeholder="请输入领导人ID" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="update_form">
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">ID</label>
                    <div class="layui-input-block">
                        <input type="text" id="update_id" name="update_id" lay-verify="title"
                               autocomplete="off" placeholder="请输入ID" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block">
                        <input type="text" id="update_name" name="update_name" lay-verify="title"
                               autocomplete="off" placeholder="请输入部门" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">详情</label>
                    <div class="layui-input-block">
                        <input type="text" id="update_description" name="update_description" lay-verify="title"
                               autocomplete="off" placeholder="请输入详情" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">领导人ID</label>
                    <div class="layui-input-block">
                        <input type="text" id="update_leader_id" name="update_leader_id" lay-verify="title"
                               autocomplete="off" placeholder="请输入领导人ID" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
    </form>
</script>



<script type="text/html" id="department-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="host-enable"
           checked="{{ d.enable == 0 ? 'true' : 'false' }}">
</script>

<script src="../component/layui/layui.js"></script>
<script src="../component/pear/pear.js"></script>
<script src="../component/jquery/jquery-3.6.0.js" charset="utf-8"></script>

<script>
    layui.config({
        version: '1554901098009' //为了更新 js 缓存，可忽略
    });

    $("#iReset").click(function () {
        $("#departmentname").val("")
        tableFlush();
    });

    $("#searchname").click(function () {
        let departmentname = $("#departmentname").val();
        tableFlush(departmentname);
    });

    function tableFlush(departmentname) {
        layui.use(['table', 'form',  'common','util'], function () {
            let table = layui.table;
            let layer = layui.layer ;//弹层
            let util = layui.util;
            let common = layui.common;
            if (departmentname != null && departmentname != '') {
                let search_json ={};
                search_json.name = departmentname;
                search_json.page=1;
                table.render({
                    elem: '#department-table',
                    url: '/department/finddepartmentbyname',
                    where:search_json,
                    parseData:function(res){
                        //console.log(res);
                        return{
                            "code":0,
                            "msg": "",
                            "data":res.res,
                        };
                    },
                    page: true,
                    title: '主机表',
                    cols: [[
                        {type: 'checkbox', fixed: 'left'}
                        , {
                            field: 'departmentId', title: '部门ID', unresize: true,templet: function (res) {
                                console.log(res);
                                return '<em>' + res.departmentId+ '</em>';

                            }
                        }
                        , {
                            field: 'departmentName', title: '部门', templet: function (res) {
                                return '<em>' + res.departmentName + '</em>'
                            }
                        }
                        , {
                            field: 'departmentDescription', title: '部门详情',  templet:  function (res) {
                                return  '<em>' +  res.departmentDescription + '</em>'
                            }
                        }
                        , {
                            field: 'departmentLeaderId', title: '领导者',   templet: function (res) {
                                return  '<em>' +  res.departmentLeaderId + '</em>'
                            }
                        }

                        , {fixed: 'right', title: '操作', toolbar: '#department-bar', width: 150}
                    ]],
                    skin: 'line',
                    toolbar: '#department-toolbar',
                    defaultToolbar: [{
                        title: '刷新',
                        layEvent: 'refresh',
                        icon: 'layui-icon-refresh',
                    }, 'filter', 'print', 'exports']
                });
            }
            else {
                table.render({
                    elem: '#department-table',
                    url: '/department/findalldepartment',
                    page: true,
                    title: '部门表',
                    parseData:function(res){
                        //console.log(res);
                        return{
                            "code":0,
                            "msg": "",
                            "data":res.res,
                        };
                    },
                    cols: [[
                        {type: 'checkbox', fixed: 'left'}
                        , {
                            field: 'departmentId', title: '部门ID', unresize: true,templet: function (res) {
                                console.log(res);
                                return '<em>' + res.departmentId+ '</em>';

                            }
                        }
                        , {
                            field: 'departmentName', title: '部门', templet: function (res) {
                                return '<em>' + res.departmentName + '</em>'
                            }
                        }
                        , {
                            field: 'departmentDescription', title: '部门详情',  templet:  function (res) {
                                return  '<em>' +  res.departmentDescription + '</em>'
                            }
                        }
                        , {
                            field: 'departmentLeaderId', title: '领导者',   templet: function (res) {
                                return  '<em>' +  res.departmentLeaderId + '</em>'
                            }
                        }

                        , {fixed: 'right', title: '操作', toolbar: '#department-bar', width: 150}
                    ]],
                    skin: 'line',
                    toolbar: '#department-toolbar',
                    defaultToolbar: [{
                        title: '刷新',
                        layEvent: 'refresh',
                        icon: 'layui-icon-refresh',
                    }, 'filter', 'print', 'exports']
                });
            }




            //头工具栏事件
            table.on('toolbar(department-table)', function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'getCheckData':
                        var data = checkStatus.data;             // jsonArray
                        layer.alert(JSON.stringify(data));       // 把一个jsonArray转换为字符串输出
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：'+ data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;
                    case 'insertdepartment':   // 新增用户
                        layer.open({     // 弹出框处理  模态框
                            type: 1               // 模态框
                            ,title: '新增部门' //不显示标题栏
                            ,closeBtn: false
                            ,area: ['500px','500px']  // 宽和高
                            ,shade: 0.5
                            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                            ,btn: ['新增', '取消']
                            ,btnAlign: 'c'
                            ,moveType: 1 //拖拽模式，0或者1
                            ,content: $('#insert_form').html()         // 引入自定义表单
                            ,btn1: function(index, layero){            // 这个是新增按钮点击事件
                                // layer.msg("新增按钮执行");
                                let departmentId = $("#insert_id").val();
                                let departmentName = $("#insert_name").val();
                                let departmentDescription = $("#insert_description").val();
                                let  departmentLeaderId = $("#insert_leader_id").val();
                                $.ajax({
                                    url:"/department/insert",           // 请求路径
                                    type:"post",                  // 请求的方式，不区分大小写
                                    cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                                    contentType:"application/json;charset=utf-8",
                                    data: JSON.stringify({
                                        departmentId:departmentId,
                                        departmentName:departmentName,
                                        departmentDescription:departmentDescription,
                                        departmentLeaderId:departmentLeaderId
                                    }),
                                    datatype:"json",        // 返回类型，text文本、html页面、json数据
                                    success:function(response){
                                        console.log("返回: " + response);
                                        if(response.code === 200) {
                                            layer.msg("新增部门成功!");
                                            layer.close(index);                         // 关闭当前窗口
                                            tableFlush();
                                        }
                                    },
                                    error:function(response){
                                        console.log("出错返回: " + response);
                                    }
                                });
                            }
                            ,success: function(layero){

                            }
                        });
                        break;
                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });



            table.on('tool(department-table)', function (obj) {
                let data = obj.data;
                console.log(JSON.stringify(data));

                if (obj.event === 'remove') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "/department/deletebyid?id="+ data.departmentId,       // 请求路径
                            type:"GET",
                            contentType:"application/json",//设置请求参数类型为json字符串
                            cache: false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                            datatype: "json",             // 返回类型，text文本、html页面、json数据
                            success: function (response) {
                                console.log("返回: " + response);
                                if (response.code === 200) {
                                    layer.msg("删除用户成功!");
                                    layer.close(index);                         // 关闭当前窗口
                                    tableFlush();
                                }
                            },
                            error: function (response) {
                                console.log("出错返回: " + response);
                            }
                        });
                    });
                } else if (obj.event === 'edit') {
                    layer.open({     // 弹出框处理  模态框
                        type: 1               // 模态框
                        ,title: '更新部门' //不显示标题栏
                        ,closeBtn: false
                        ,area: ['500px','500px']  // 宽和高
                        ,shade: 0.5
                        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                        ,btn: ['更新', '取消']
                        ,btnAlign: 'c'
                        ,moveType: 1 //拖拽模式，0或者1
                        ,content: $('#update_form').html()
                        ,btn1: function(index, layero){            // 这个是新增按钮点击事件

                            let departmentId = $("#update_id").val();
                            let departmentName = $("#update_name").val();
                            let departmentDescription = $("#update_description").val();
                            let  departmentLeaderId = $("#update_leader_id").val();

                            $.ajax({
                                url:"/host/editbymap",           // 请求路径
                                type:"post",                  // 请求的方式，不区分大小写
                                cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                                contentType:"application/json;charset=utf-8",
                                data: JSON.stringify({
                                    departmentId:departmentId,
                                    departmentName:departmentName,
                                    departmentDescription:departmentDescription,
                                    departmentLeaderId:departmentLeaderId
                                }),
                                datatype:"json",        // 返回类型，text文本、html页面、json数据
                                success:function(response){
                                    console.log("返回: " + response);
                                    if(response.code === 200) {
                                        layer.msg("更新部门成功!");
                                        layer.close(index);                         // 关闭当前窗口
                                        tableFlush();
                                    }
                                },
                                error:function(response){
                                    console.log("出错返回: " + response);
                                }
                            });
                        }
                        ,success: function(layero){
                            $("#update_id").val(data.departmentId);
                            $("#update_name").val(data.departmentName);
                            $("#update_description").val(data.departmentDescription);
                            $("#update_leader_id").val(data.departmentLeaderId);

                                },
                                error:function(response){
                                    console.log("出错返回: " + response);
                                }
                            });


                        }
                    });

            });
    }
    tableFlush();
</script>
</body>
</html>
